package tgc.rj.zz.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.rj.zz.custom.DataGridUtils;
import tgc.rj.zz.entity.Journal;
import tgc.rj.zz.entity.LesseeAdmin;
import tgc.rj.zz.entity.SysRole;
import tgc.rj.zz.entity.SysUser;
import tgc.rj.zz.form.LesseeAdminForm;
import tgc.rj.zz.repository.RoleRepository;
import tgc.rj.zz.repository.SysUserRepository;
import tgc.rj.zz.security.UserUtils;
import tgc.rj.zz.service.JournalService;
import tgc.rj.zz.service.LesseeAdminService;

@Controller
@RequestMapping(value="lesseeAdmin")
public class LesseeAdminController {
	@Autowired
	private LesseeAdminService lesseeAdminDAO;
	@Autowired
	private UserUtils userUtils;
	@Autowired
	private SysUserRepository userDAO;
	@Autowired
	private JournalService service;
	@Autowired
	private RoleRepository roleDAO;

	/**
	 * 对所有的租户管理员进行管理
	 * @return
	 */
	@RequestMapping(value="all")
	public String all() {
		return "system/lesseeAdmin";
	}
	
	@RequestMapping(value="/list")
	@ResponseBody
	public Object list(LesseeAdminForm form) {
		Pageable pageable = form.buildPageable();
		Page<LesseeAdmin> page = null;
		if(form.getId() == null) {
			page = lesseeAdminDAO.findAll(pageable);			
		}else {
			page = lesseeAdminDAO.findAll(form.buildSpes(), pageable);
		}
		HashMap<String , Object> result = DataGridUtils.getPageResult(page);
		return result;
	}
	@RequestMapping(value="/saveAdmin")
	@ResponseBody
	public Object save(LesseeAdmin model) {
		BCryptPasswordEncoder encoder=new BCryptPasswordEncoder(); 
		SysRole role=roleDAO.findById(2).get();
		SysUser user=userUtils.getUser();
		if((model.getPassword()==null||model.getPassword()=="")) {
			model.setPassword(encoder.encode("123"));
			
		}
		model.getRoles().add(role);
		userDAO.save(model);
		
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		String date = df.format(new Date());
		Journal journal = new Journal();
		journal.setDate(date);
		journal.setUsername(user.getUsername());
		journal.setOperationName("编辑了租户管理员");
		service.save(journal);
		return "ok";
	}
	@RequestMapping(value="/deleteAdmin")
	@ResponseBody
	public void deleteAdmin(String ids) {
		SysUser user = userUtils.getUser();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		String date = df.format(new Date());
		Journal journal = new Journal();
		journal.setDate(date);
		journal.setUsername(user.getUsername());
		journal.setOperationName("删除了租户管理员");
		service.save(journal);
		String[] id = ids.split(",");
		for(int i = 0; i < id.length; i++) {
			userDAO.deleteById(Integer.parseInt(id[i]));
		}
	}
	
	
	/**
	 * 修改密码，三级公用
	 * @param pad
	 * @return
	 */
	@RequestMapping(value="/changePwd")
	public String update() {
		return "admin/changePwd";
	}
	@RequestMapping(value="/xg")
	public String xg(String pad) {
		SysUser user = userUtils.getUser();
		BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
		user.setPassword(encoder.encode(pad));
		userDAO.save(user);
		
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		String date = df.format(new Date());
		Journal journal = new Journal();
		journal.setDate(date);
		journal.setUsername(user.getUsername());
		journal.setOperationName("修改密码");
		service.save(journal);
		return "redirect:../logout";
	}
	@RequestMapping(value="/pwd")
	@ResponseBody
	public Boolean pwd(String password) {
		SysUser user = userUtils.getUser();
		BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
		boolean f = encoder.matches(password,user.getPassword());
		return f;
	}
	
}
